package org.forwhat.dao.Impl;

import org.forwhat.bean.WhatUser;
import org.forwhat.dao.WhatUserDao;
import org.forwhat.framework.annotation.Data;
import org.forwhat.framework.annotation.InsertBean;

/**
 * @projectName: ForWhat
 * @package: org.forwhat.dao.Impl
 * @className: WhatUserDaoImpl
 * @author: LGJ
 * @description: TODO
 * @date: 2023/11/25 20:12
 * @version: 1.0
 */
//@Data("WhatUserDaoImpl")
public class WhatUserDaoImpl implements WhatUserDao {
    @InsertBean("BaseDaoImpl")
    private BaseDaoImpl baseDaoImpl;

    public BaseDaoImpl getBaseDaoImpl() {
        return baseDaoImpl;
    }

    public void setBaseDaoImpl(BaseDaoImpl baseDaoImpl) {
        this.baseDaoImpl = baseDaoImpl;
    }

    @Override
    public WhatUser selectUser(String email, String password) {
        try {
            return baseDaoImpl.selectBean("select * from what_user where user_email = ? and user_password = ? ", WhatUser.class, email, password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getLatestUser() {
        try {
            return (String) baseDaoImpl.selectObject("select user_id from what_user order by user_create_time desc limit 1");
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String getUserNameById(String userId) {
        if (userId == null || userId.equals("")) {
            return null;
        }
        try {
            return (String) baseDaoImpl.selectObject("select user_name from what_user where user_id = ?", userId);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public String selectEmail(String email) {
        try {
            return (String) baseDaoImpl.selectObject("select user_email from what_user where user_email = ?", email);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int addUser(WhatUser whatUser) {

        return baseDaoImpl.update("insert into what_user(user_id, user_name, user_email, user_password, user_sex, user_png, user_create_time) value (?,?,?,?,?,?,?)",
                whatUser.getUserId(),
                whatUser.getUserName(),
                whatUser.getUserEmail(),
                whatUser.getUserPassword(),
                whatUser.getUserSex(),
                whatUser.getUserPng(),
                whatUser.getUserCreateTime()
        );

    }
}
